package cn.ultrarisk.scadmin.manager.biz.stat.service;

import cn.ultrarisk.mybatis.extension.query.Query;
import cn.ultrarisk.scadmin.manager.biz.monitor.dao.SolrNodeMonitorDAO;
import cn.ultrarisk.scadmin.manager.biz.stat.context.StatType;
import cn.ultrarisk.scadmin.manager.biz.stat.dao.SolrNodeStatDAO;
import cn.ultrarisk.scadmin.manager.biz.stat.entity.SolrNodeStat;
import cn.ultrarisk.scadmin.manager.util.CollectionUtil;
import cn.ultrarisk.scadmin.manager.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * solr节点统计数据操作类
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/3/28
 * @since V1.0
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class SolrNodeStatService {
    @Autowired
    private SolrNodeStatDAO solrNodeStatDAO;
    @Autowired
    private SolrNodeMonitorDAO solrNodeMonitorDAO;

    public void buildHourStat(long time) {
        long startTime = DateUtil.getHourBeginTime(time);
        long endTime = DateUtil.getHourEndTime(time);

        Map<Integer, Map<String, Double>> loadStat = solrNodeMonitorDAO.getLoadStat(startTime, endTime);
        Map<Integer, Map<String, Long>> memStat = solrNodeMonitorDAO.getMemStat(startTime, endTime);

        Map<Integer, SolrNodeStat> statMap = new HashMap<Integer, SolrNodeStat>();
        if (loadStat != null) {
            for (Integer nodeId : loadStat.keySet()) {
                SolrNodeStat stat = statMap.get(nodeId);
                if (stat == null) {
                    stat = new SolrNodeStat();
                    stat.setNodeId(nodeId);
                }
                Map<String, Double> nodeLoadMap = loadStat.get(nodeId);
                if (nodeLoadMap != null) {
                    stat.setMaxLoad(nodeLoadMap.get("max"));
                    stat.setAvgLoad(nodeLoadMap.get("avg"));
                }
                statMap.put(nodeId, stat);
            }
        }

        if (memStat != null) {
            for (Integer nodeId : memStat.keySet()) {
                SolrNodeStat stat = statMap.get(nodeId);
                if (stat == null) {
                    stat = new SolrNodeStat();
                    stat.setNodeId(nodeId);
                }
                Map<String, Long> nodeMemusageMap = memStat.get(nodeId);
                if (nodeMemusageMap != null) {
                    stat.setMaxMemusage(nodeMemusageMap.get("max"));
                    stat.setAvgMemusage(nodeMemusageMap.get("avg"));
                }
                statMap.put(nodeId, stat);
            }
        }

        if (statMap.size() > 0) {
            for (SolrNodeStat stat : statMap.values()) {
                stat.setType(StatType.HOUR);
                stat.setStartTime(startTime);
                stat.setEndTime(endTime);
            }
            solrNodeStatDAO.createBatch(statMap.values());
        }
    }

    public void deleteHourStat(long time) {
        Query query = Query.query(SolrNodeStat.TYPE, StatType.HOUR)
                .gte(SolrNodeStat.START_TIME, DateUtil.getHourBeginTime(time)).lt(SolrNodeStat.START_TIME, DateUtil.getHourEndTime(time));
        solrNodeStatDAO.delete(query);
    }

    public void buildDayStat(long time) {
        long startTime = DateUtil.getDayBeginTime(time);
        long endTime = DateUtil.getDayEndTime(time);
        List<SolrNodeStat> stats = solrNodeStatDAO.buildStat(startTime, endTime);
        if (CollectionUtil.isNotEmpty(stats)) {
            for (SolrNodeStat stat : stats) {
                stat.setType(StatType.DAY);
            }
            solrNodeStatDAO.createBatch(stats);
        }
    }

    public void deleteDayStat(long time) {
        Query query = Query.query(SolrNodeStat.TYPE, StatType.DAY)
                .gte(SolrNodeStat.START_TIME, DateUtil.getDayBeginTime(time)).lt(SolrNodeStat.START_TIME, DateUtil.getDayEndTime(time));
        solrNodeStatDAO.delete(query);
    }

    public void buildMonthStat(long time) {
        long startTime = DateUtil.getMonthBeginTime(time);
        long endTime = DateUtil.getMonthEndTime(time);
        List<SolrNodeStat> stats = solrNodeStatDAO.buildStat(startTime, endTime);
        if (CollectionUtil.isNotEmpty(stats)) {
            for (SolrNodeStat stat : stats) {
                stat.setType(StatType.MONTH);
            }
            solrNodeStatDAO.createBatch(stats);
        }
    }

    public void deleteMonthStat(long time) {
        Query query = Query.query(SolrNodeStat.TYPE, StatType.MONTH)
                .gte(SolrNodeStat.START_TIME, DateUtil.getMonthBeginTime(time)).lt(SolrNodeStat.START_TIME, DateUtil.getMonthEndTime(time));
        solrNodeStatDAO.delete(query);
    }
}